package com.chengma.controller;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.chengma.common.*;
import com.chengma.domain.TbComment;
import com.chengma.domain.vo.CommentTreeDTO;
import com.chengma.service.ITbCommentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

/**
 * 评论Controller
 *
 * @author tck
 * @date 2025-03-19
 */
@RestController
@RequestMapping("/system/comment")
public class TbCommentController extends BaseController {
    @Autowired
    private ITbCommentService tbCommentService;

    /**
     * 查询评论列表
     */
    @GetMapping("/list")
    public TableDataInfo list(TbComment tbComment) {
        List<TbComment> list = tbCommentService.selectTbCommentList(tbComment);
        return getDataTable(list);
    }


    /**
     * 获取评论详细信息
     */
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return success(tbCommentService.selectTbCommentById(id));
    }

    /**
     * 新增评论
     */
    @PostMapping
    public AjaxResult add(@RequestBody TbComment tbComment) {
        return toAjax(tbCommentService.insertTbComment(tbComment));
    }

    /**
     * 修改评论
     */
    @PutMapping
    public AjaxResult edit(@RequestBody TbComment tbComment) {
        return toAjax(tbCommentService.updateTbComment(tbComment));
    }

    /**
     * 删除评论
     */
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(tbCommentService.deleteTbCommentByIds(ids));
    }

    @Autowired
    public TbCommentController(ITbCommentService commentService) {
        this.tbCommentService = commentService;
    }

    /**
     * 获取问题的评论树
     * @param questionId 问题ID (路径变量)
     * @return 评论树结构
     */
    @GetMapping("/tree/{questionId}")
    public ApiResponse<List<CommentTreeDTO>> getCommentTree(
            @PathVariable String questionId) {
        return ApiResponse.success(tbCommentService.getCommentTree(questionId));
    }

    //  分页接口
    @GetMapping("/list/{questionId}")
    public ResponseEntity<ApiResponse<PageResult<CommentTreeDTO>>> getComments(
            @PathVariable String questionId,
            @RequestParam(defaultValue = "1") int page,
            @RequestParam(defaultValue = "10") int size) {

        PageResult<CommentTreeDTO> pageResult = tbCommentService.getComments(questionId, page, size);
        return ResponseEntity.ok(ApiResponse.success(pageResult));
    }


}
